﻿/**************************************************
 * 文 件 名：DbHelper.cs
 * 版    本：V1.0
 * 创 建 人：杨书林
 * 日    期：2011-7-12
 * 说    明：类DbHelper为数据访问静态类
 
 * 修 改 人：
 * 日    期：
 * 描    述：
 **************************************************/
using System.Data;

namespace Ysl.Data.DbHelper
{
    /// <summary>
    /// DbHelper
    /// </summary>
    public sealed class DbHelper
    {
        /// <summary>
        /// 默认静态IDbHelper对象
        /// </summary>
        private static readonly IDbHelper dbHelper = DbHelperFactory.CreateDbHelper();

        #region 根据连接名称实例化一个新的IDbHelper对象
        /// <summary>
        /// 根据连接名称实例化一个新的IDbHelper对象
        /// </summary>
        /// <param name="connectionName">连接名称</param>
        /// <returns>返回IDbHelper类型</returns>
        public static IDbHelper Instance(string connectionName)
        {
            return DbHelperFactory.CreateDbHelper(connectionName);
        }
        #endregion

        #region 开始/执行/回滚事务操作
        /// <summary>
        /// 开始事务操作
        /// </summary>
        public static void BeginTransaction()
        {
            dbHelper.BeginTransaction();
        }

        /// <summary>
        /// 执行事务操作
        /// </summary>
        public static void Commit()
        {
            dbHelper.Commit();
        }

        /// <summary>
        /// 回滚事务操作
        /// </summary>
        public static void Rollback()
        {
            dbHelper.Rollback();
        }
        #endregion

        #region 组织参数及相关方法
        /// <summary>
        /// 根据参数名和值组织参数
        /// </summary>
        /// <param name="parameterName">参数名称</param>
        /// <param name="parameterValue">值</param>
        /// <returns>返回IDbDataParameter类型</returns>
        public static IDbDataParameter MakeInParameter(string parameterName, object parameterValue)
        {
            return dbHelper.MakeInParameter(parameterName, parameterValue);
        }

        /// <summary>
        /// 根据参数名和值组织参数
        /// </summary>
        /// <param name="parameterName">参数名称</param>
        /// <param name="dbType">参数类型</param>
        /// <param name="parameterValue">值</param>
        /// <returns>返回IDbDataParameter类型</returns>
        public static IDbDataParameter MakeInParameter(string parameterName, DbType dbType, object parameterValue)
        {
            return dbHelper.MakeInParameter(parameterName, dbType, 0, parameterValue);
        }

        /// <summary>
        /// 根据参数名和值组织参数
        /// </summary>
        /// <param name="parameterName">参数名称</param>
        /// <param name="dbType">参数类型</param>
        /// <param name="size">类型长度</param>
        /// <param name="parameterValue">值</param>
        /// <returns>返回IDbDataParameter类型</returns>
        public static IDbDataParameter MakeInParameter(string parameterName, DbType dbType, int size, object parameterValue)
        {
            return dbHelper.MakeParameter(parameterName, dbType, size, ParameterDirection.Input, parameterValue);
        }

        /// <summary>
        /// 根据参数名和值组织参数
        /// </summary>
        /// <param name="parameterName">参数名称</param>
        /// <param name="dbType">参数类型</param>
        /// <param name="size">类型长度</param>
        /// <returns>返回IDbDataParameter类型</returns>
        public static IDbDataParameter MakeOutParameter(string parameterName, DbType dbType, int size)
        {
            return dbHelper.MakeParameter(parameterName, dbType, size, ParameterDirection.Output, null);
        }

        /// <summary>
        /// 根据参数名和值组织参数
        /// </summary>
        /// <param name="parameterName">参数名称</param>
        /// <param name="dbType">参数类型</param>
        /// <param name="size">类型长度</param>
        /// <returns>返回IDbDataParameter类型</returns>
        public static IDbDataParameter MakeReturnParameter(string parameterName, DbType dbType, int size)
        {
            return dbHelper.MakeParameter(parameterName, dbType, size, ParameterDirection.ReturnValue, null);
        }

        /// <summary>
        /// 根据参数名和值组织参数
        /// </summary>
        /// <param name="parameterName">参数名称</param>
        /// <param name="dbType">参数类型</param>
        /// <param name="size">类型长度</param>
        /// <param name="parameterDirection">参数的输入、输出类型</param>
        /// <param name="parameterValue">值</param>
        /// <returns>返回IDbDataParameter类型</returns>
        public static IDbDataParameter MakeParameter(string parameterName, DbType dbType, int size, ParameterDirection parameterDirection, object parameterValue)
        {
            return dbHelper.MakeParameter(parameterName, dbType, size, parameterDirection, parameterValue);
        }

        /// <summary>
        /// 根据参数名和值组织参数
        /// </summary>
        /// <param name="parameterNames">参数名称</param>
        /// <param name="parameterValues">值</param>
        /// <returns>返回IDbDataParameter[]类型</returns>
        public static IDbDataParameter[] MakeInParameters(string[] parameterNames, object[] parameterValues)
        {
            return dbHelper.MakeInParameters(parameterNames, parameterValues);

        }

        /// <summary>
        /// 根据存储过程和值组织参数
        /// </summary>
        /// <param name="procedureName">存储过程名称</param>
        /// <param name="parameterValues">值</param>
        /// <returns>返回IDbDataParameter[]类型</returns>
        public static IDbDataParameter[] MakeProcedureParameters(string procedureName, object[] parameterValues)
        {
            return dbHelper.MakeProcedureParameters(procedureName, parameterValues);
        }

        /// <summary>
        /// 获取存储过程参数集（不包括返回值参数）
        /// </summary>
        /// <param name="procedureName">存储过程名称</param>
        /// <returns>返回IDbDataParameter[]类型</returns>
        public static IDbDataParameter[] GetProcedureParameters(string procedureName)
        {
            return dbHelper.GetProcedureParameters(procedureName);
        }

        /// <summary>
        /// 获取存储过程参数集
        /// </summary>
        /// <param name="procedureName">存储过程名称</param>
        /// <param name="includeReturnValueParameter">是否包含返回值参数（true表示包含，false表示不包含，默认为false）</param>
        /// <returns>返回IDbDataParameter[]类型</returns>
        public static IDbDataParameter[] GetProcedureParameters(string procedureName, bool includeReturnValueParameter)
        {
            return dbHelper.GetProcedureParameters(procedureName, includeReturnValueParameter);
        }

        /// <summary>
        /// 为参数赋值
        /// </summary>
        /// <param name="dbParameters">参数</param>
        /// <param name="parameterValues">值</param>
        public static void AssignParameters(IDbDataParameter[] dbParameters, object[] parameterValues)
        {
            dbHelper.AssignParameters(dbParameters, parameterValues);
        }
        #endregion

        #region ExecuteNonQuery
        /// <summary>
        /// 对连接执行SQL语句并返回受影响的行数
        /// </summary>
        /// <param name="sqlText">SQL语句</param>
        /// <returns>返回受影响的行数</returns>
        public static int ExecuteNonQuerySql(string sqlText)
        {
            return dbHelper.ExecuteNonQuerySql(sqlText, null);
        }

        /// <summary>
        /// 对连接执行SQL语句并返回受影响的行数
        /// </summary>
        /// <param name="sqlText">SQL语句</param>
        /// <param name="dbParameters">参数</param>
        /// <returns>返回受影响的行数</returns>
        public static int ExecuteNonQuerySql(string sqlText, params IDbDataParameter[] dbParameters)
        {
            return dbHelper.ExecuteNonQuery(CommandType.Text, sqlText, dbParameters);
        }

        /// <summary>
        /// 对连接执行存储过程并返回受影响的行数
        /// </summary>
        /// <param name="procedureName">存储过程名称</param>
        /// <returns>返回受影响的行数</returns>
        public static int ExecuteNonQueryProcedure(string procedureName)
        {
            return dbHelper.ExecuteNonQueryProcedure(procedureName, null);
        }

        /// <summary>
        /// 对连接执行存储过程并返回受影响的行数
        /// </summary>
        /// <param name="procedureName">存储过程名称</param>
        /// <param name="parameterValues">参数值</param>
        /// <returns>返回受影响的行数</returns>
        public static int ExecuteNonQueryProcedure(string procedureName, params object[] parameterValues)
        {
            IDbDataParameter[] dbParameters = dbHelper.MakeProcedureParameters(procedureName, parameterValues);
            return dbHelper.ExecuteNonQuery(CommandType.StoredProcedure, procedureName, dbParameters);
        }

        /// <summary>
        /// 对链接执行操作并返回受影响的行数
        /// </summary>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="commandText">命令字符串</param>
        /// <returns>返回受影响的行数</returns>
        public static int ExecuteNonQuery(CommandType commandType, string commandText)
        {
            return dbHelper.ExecuteNonQuery(commandType, commandText, null);
        }

        /// <summary>
        /// 对链接执行操作并返回受影响的行数
        /// </summary>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="commandText">命令字符串</param>
        /// <param name="dbParameters">参数</param>
        /// <returns>返回受影响的行数</returns>
        public static int ExecuteNonQuery(CommandType commandType, string commandText, params IDbDataParameter[] dbParameters)
        {
            return dbHelper.ExecuteNonQuery(commandType, commandText, dbParameters);
        }
        #endregion

        #region ExecuteFill
        /// <summary>
        /// 填充数据表
        /// </summary>
        /// <param name="sqlText">SQL语句</param>
        /// <returns>返回数据表</returns>
        public static DataTable ExecuteFillSql(string sqlText)
        {
            return dbHelper.ExecuteFillSql(sqlText, string.Empty);
        }

        /// <summary>
        /// 填充数据表
        /// </summary>
        /// <param name="sqlText">SQL语句</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回数据表</returns>
        public static DataTable ExecuteFillSql(string sqlText, string tableName)
        {
            return dbHelper.ExecuteFillSql(sqlText, tableName, null);
        }

        /// <summary>
        /// 填充数据表
        /// </summary>
        /// <param name="sqlText">SQL语句</param>
        /// <param name="dbParameters">参数</param>
        /// <returns>返回数据表</returns>
        public static DataTable ExecuteFillSql(string sqlText, params IDbDataParameter[] dbParameters)
        {
            return dbHelper.ExecuteFillSql(sqlText, string.Empty, dbParameters);
        }

        /// <summary>
        /// 填充数据表
        /// </summary>
        /// <param name="sqlText">SQL语句</param>
        /// <param name="tableName">表名</param>
        /// <param name="dbParameters">参数</param>
        /// <returns>返回数据表</returns>
        public static DataTable ExecuteFillSql(string sqlText, string tableName, params IDbDataParameter[] dbParameters)
        {
            return dbHelper.ExecuteFill(CommandType.Text, sqlText, tableName, dbParameters);
        }

        /// <summary>
        /// 填充数据表
        /// </summary>
        /// <param name="procedureName">存储过程</param>
        /// <returns>返回数据表</returns>
        public static DataTable ExecuteFillProcedure(string procedureName)
        {
            return dbHelper.ExecuteFillProcedure(procedureName, string.Empty);
        }

        /// <summary>
        /// 填充数据表
        /// </summary>
        /// <param name="procedureName">存储过程</param>
        /// <param name="tableName">表名</param>
        /// <returns>返回数据表</returns>
        public static DataTable ExecuteFillProcedure(string procedureName, string tableName)
        {
            return dbHelper.ExecuteFillProcedure(procedureName, tableName, null);
        }

        /// <summary>
        /// 填充数据表
        /// </summary>
        /// <param name="procedureName">存储过程</param>
        /// <param name="parameterValues">参数值</param>
        /// <returns>返回数据表</returns>
        public static DataTable ExecuteFillProcedure(string procedureName, params object[] parameterValues)
        {
            return dbHelper.ExecuteFillProcedure(procedureName, string.Empty, parameterValues);
        }

        /// <summary>
        /// 填充数据表
        /// </summary>
        /// <param name="procedureName">存储过程</param>
        /// <param name="tableName">表名</param>
        /// <param name="parameterValues">参数值</param>
        /// <returns>返回数据表</returns>
        public static DataTable ExecuteFillProcedure(string procedureName, string tableName, params object[] parameterValues)
        {
            return dbHelper.ExecuteFillProcedure(procedureName, tableName, parameterValues);
        }

        /// <summary>
        /// 填充数据表
        /// </summary>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="commandText">命令字符串</param>
        /// <param name="dbParameters">参数</param>
        /// <returns>返回数据表</returns>
        public static DataTable ExecuteFill(CommandType commandType, string commandText, params IDbDataParameter[] dbParameters)
        {
            return dbHelper.ExecuteFill(commandType, commandText, null, dbParameters);
        }

        /// <summary>
        /// 填充数据表
        /// </summary>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="commandText">命令字符串</param>
        /// <param name="tableName">表名</param>
        /// <param name="dbParameters">参数</param>
        /// <returns>返回数据表</returns>
        public static DataTable ExecuteFill(CommandType commandType, string commandText, string tableName, params IDbDataParameter[] dbParameters)
        {
            return dbHelper.ExecuteFill(commandType, commandText, tableName, dbParameters);
        }
        #endregion

        #region ExecuteReader
        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="sqlText">SQL语句</param>
        /// <returns>返回IDataReader类型</returns>
        public static IDataReader ExecuteReaderSql(string sqlText)
        {
            return dbHelper.ExecuteReaderSql(sqlText, null);
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="sqlText">SQL语句</param>
        /// <param name="dbParameters">参数</param>
        /// <returns>返回IDataReader类型</returns>
        public static IDataReader ExecuteReaderSql(string sqlText, params IDbDataParameter[] dbParameters)
        {
            return dbHelper.ExecuteReader(CommandType.Text, sqlText, dbParameters);
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="procedureName">存储过程名称</param>
        /// <returns>返回IDataReader类型</returns>
        public static IDataReader ExecuteReaderProcedure(string procedureName)
        {
            return dbHelper.ExecuteReaderProcedure(procedureName, null);
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="procedureName">存储过程名称</param>
        /// <param name="parameterValues">参数值</param>
        /// <returns>返回IDataReader类型</returns>
        public static IDataReader ExecuteReaderProcedure(string procedureName, params object[] parameterValues)
        {
            IDbDataParameter[] dbParameters = dbHelper.MakeProcedureParameters(procedureName, parameterValues);
            return dbHelper.ExecuteReader(CommandType.StoredProcedure, procedureName, dbParameters);
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="commandText">命令字符串</param>
        /// <returns>返回IDataReader类型</returns>
        public static IDataReader ExecuteReader(CommandType commandType, string commandText)
        {
            return dbHelper.ExecuteReader(commandType, commandText, null);
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="commandText">命令字符串</param>
        /// <param name="dbParameters">参数</param>
        /// <returns>返回IDataReader类型</returns>
        public static IDataReader ExecuteReader(CommandType commandType, string commandText, params IDbDataParameter[] dbParameters)
        {
            return dbHelper.ExecuteReader(commandType, commandText, dbParameters);
        }
        #endregion

        #region ExecuteScalar
        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="sqlText">SQL语句</param>
        /// <returns>返回object类型</returns>
        public static object ExecuteScalarSql(string sqlText)
        {
            return dbHelper.ExecuteScalarSql(sqlText, null);
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="sqlText">SQL语句</param>
        /// <param name="dbParameters">参数</param>
        /// <returns>返回object类型</returns>
        public static object ExecuteScalarSql(string sqlText, params IDbDataParameter[] dbParameters)
        {
            return dbHelper.ExecuteScalar(CommandType.Text, sqlText, dbParameters);
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="procedureName">存储过程名称</param>
        /// <returns>返回object类型</returns>
        public static object ExecuteScalarProcedure(string procedureName)
        {
            return dbHelper.ExecuteScalarProcedure(procedureName, null);
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="procedureName">存储过程名称</param>
        /// <param name="parameterValues">参数值</param>
        /// <returns>返回object类型</returns>
        public static object ExecuteScalarProcedure(string procedureName, params object[] parameterValues)
        {
            IDbDataParameter[] dbParameters = dbHelper.MakeProcedureParameters(procedureName, parameterValues);
            return dbHelper.ExecuteScalar(CommandType.StoredProcedure, procedureName, dbParameters);
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="commandText">命令字符串</param>
        /// <returns>返回object类型</returns>
        public static object ExecuteScalar(CommandType commandType, string commandText)
        {
            return dbHelper.ExecuteScalar(commandType, commandText, null);
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="commandType">命令字符串类型</param>
        /// <param name="commandText">命令字符串</param>
        /// <param name="dbParameters">参数</param>
        /// <returns>返回object类型</returns>
        public static object ExecuteScalar(CommandType commandType, string commandText, params IDbDataParameter[] dbParameters)
        {
            return dbHelper.ExecuteScalar(commandType, commandText, dbParameters);
        }
        #endregion

    }
}
